package com.gjun.struts.action;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.Date;

import org.apache.struts2.ServletActionContext;

import com.gjun.struts.bean.GjunMaintype;
import com.gjun.struts.resource.GjunMainTypeResourceIf;
import com.gjun.struts.util.UUIDGenerator;
import com.opensymphony.xwork2.ActionSupport;

public class SaveNewsMainTypeAction extends ActionSupport {
	private String typename;

	private String info;

	private File file;

	private String fileFileName;

	private String fileContentType;

	private String dir;

	private String targetFileName;

	private String result;

	private GjunMainTypeResourceIf mainTypeResource;

	public GjunMainTypeResourceIf getMainTypeResource() {
		return mainTypeResource;
	}

	public void setMainTypeResource(GjunMainTypeResourceIf mainTypeResource) {
		this.mainTypeResource = mainTypeResource;
	}

	public String getDir() {
		return dir;
	}

	public void setDir(String dir) {
		this.dir = dir;
	}

	public File getFile() {
		return file;
	}

	public void setFile(File file) {
		this.file = file;
	}

	public String getFileContentType() {
		return fileContentType;
	}

	public void setFileContentType(String fileContentType) {
		this.fileContentType = fileContentType;
	}

	public String getFileFileName() {
		return fileFileName;
	}

	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}

	public String getInfo() {
		return info;
	}

	public void setInfo(String info) {
		this.info = info;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public String getTargetFileName() {
		return targetFileName;
	}

	public void setTargetFileName(String targetFileName) {
		this.targetFileName = targetFileName;
	}

	public String getTypename() {
		return typename;
	}

	public void setTypename(String typename) {
		this.typename = typename;
	}

	private static void copy(File src, File dst) {
		try {
			InputStream in = null;
			OutputStream out = null;
			try {
				in = new BufferedInputStream(new FileInputStream(src));
				out = new BufferedOutputStream(new FileOutputStream(dst));
				byte[] buffer = new byte[1024];
				while (in.read(buffer) > 0) {
					out.write(buffer);
				}
			} finally {
				if (null != in) {
					in.close();
				}
				if (null != out) {
					out.close();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public String saveMainType() {
//		System.out.println("============================:  " + fileFileName);
//		System.out.println("前缀:      " + getExtention(fileFileName));
//		System.out.println("----------------------------:  " + info);
//		System.out.println("&&&&&&&&&&&&&&&&&&&&&&&&&&&&:  " + typename);
		GjunMaintype maintype = new GjunMaintype();
		maintype.setInfo(info);
		maintype.setRegdate(new Date());
		maintype.setTypename(typename);
		System.out.println("文件:   "+file);
		if(file!=null){
			String fileName = new UUIDGenerator().generate().toString()
					+ getExtention(fileFileName);
			
			String fileSavePath = ServletActionContext.getRequest()
					.getRealPath("/")
					+ "newsImage/" + fileName;
			//System.out.println(ServletActionContext.getRequest().getContextPath()+"/newsImage/"+fileName);
			copy(file,new File(fileSavePath));
	
			
			
			maintype.setImage(ServletActionContext.getRequest().getContextPath()+"/newsImage/"+fileName);
		}
		try {
			mainTypeResource.saveMainType(maintype);
			result = "saveSuccess";
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			result = "saveFaild";
		}

		System.out.println(result);
		return "success";
	}
	
	
	public String checkMainTypeExist(){
		System.out.println("t:   "+typename);
		boolean isHave = mainTypeResource.loadMainTypeByName(typename);
		System.out.println(isHave);
		if(isHave)
		result = "haveMainType";
		else
		result = "noMainType";
		return "success";
	}

	private static String getExtention(String fileName) {
		int pos = fileName.lastIndexOf(".");
		return fileName.substring(pos);
	}
	
	
	public String toGBK(String oldStr){
		try {
			return new String(typename.getBytes(),"GBK");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return oldStr;
			
		}
	}

}
